<?php
namespace Home\Event;
//set_time_limit(100);
/**
 * 短信
 * Class LetterController
 * @package Home\Controller
 */

class LetterEvent {
    private $sms_apim;
    private $sms_crop_id;
    private $sms_pwd;
    private $sms_title;
    public function __construct(){
        $this->sms_apim = C('SMS_APIM');
        $this->sms_crop_id = C('SMS_CROP_ID');
        $this->sms_pwd = C('SMS_PWD');
        $this->sms_title = C('SMS_TITLE');
    }
    public function index(){
        header("content-Type: text/html; charset=GBK");
        $mobile = '15023231187';
        $content = '测试短信接口'.$this->sms_title;
        $content = iconv('UTF-8', 'GBK', $content);
        $reCode = $this->oneLetter($mobile,$content);
        dump($reCode);
    }

    /**
     * 验证验证码
     * @param $mobile
     * @param $code
     */
    public function verifyMobile($code){
        $auth_code = session('verifyCode');
        $date = session('verifyDate');
        $time = session('verifyTime');
        //比对时间
        if((time() - $time) > ($date * 60) + 10){
            return ['stat' => false,'msg' => '验证码过期！'];
        }
        //比对验证码
        return ($auth_code == $code) ? ['stat' => true,'msg' => '成功！'] : ['stat' => false,'msg' => '验证码错误！'];
    }

    /**
     * 验证手机(生成验证码)
     * @param $mobile 手机号
     * @param $time 有效时间
     */
    public function verifyMobileCode($mobile,$date){
        //有效时间入session(单位-分钟)
        session('verifyDate',$date);
        //4位验证码
        $auth_code = '';
        for($i = 0;$i < 4;$i++){
            $auth_code .= mt_rand(0,9);
        }
        //验证码入session
        session('verifyCode',$auth_code);
        session('verifyMobile',$mobile);
        //当前时间入session
        session('verifyTime',time());
        $content = '验证码为：'.$auth_code.'。有效时间为：'.$date.'分钟，如非本人操作请忽略。';
        $stat = $this->sendLetter($mobile,$content);
        if($stat['status'] === true){
            return ['data' => $date, 'stat' => true] ;
        }else{
            // 短信发送失败记录日志
            $add_data = [
                'mobile' => $mobile,
                'type' => 2,
                'errcode' => $stat['status'],
                'errmsg' => $stat['msg'],
                'create_time' => time(),
                'status' => 2,
            ];
            M('push_message_log')->add($add_data);
            return  ['stat' => false,'msg' => $stat];
        }


    }
    /**
     * 发送短信
     * @param $mobile 手机号
     * @param $content 发送内容
     */
    public function sendLetter($mobile,$content){
        $content = $content.$this->sms_title;
        $content = iconv('UTF-8', 'GB2312', $content);
        $reCode = $this->oneLetter($mobile,$content);
        return $reCode;
    }

    /**
     * @param $access_token
     * $barcodeArr 数组
     * @return mixed
     * 发送短信
     */
    public function oneLetter($mobile,$content){
        if(!$mobile){
            return false;
        }
        $url = $this->sms_apim.'?CorpID='.$this->sms_crop_id.'&Pwd='.$this->sms_pwd.'&Mobile='.$mobile.'&Content='.$content;

        $stat = file_get_contents($url);

       switch($stat){
           case 0 :
               return ['status' => true,'msg' => '发送成功'];
           case -1 :
               //todo 写入日志 -- 帐号未注册
               return ['status' => -1,'msg' => '帐号未注册'];
           case -2 :
               //todo 写入日志 -- 其他错误
               return ['status' => -2,'msg' => '其他错误'];
           case -3 :
               //todo 写入日志 -- 密码错误
               return ['status' => -3,'msg' => '密码错误'];
           case -4 :
               //todo 写入日志 -- 手机号格式不对
               return ['status' => -4,'msg' => '手机号格式不对'];
           case -5 :
               //todo 写入日志 -- 余额不足
               return ['status' => -5,'msg' => '余额不足'];
           case -6 :
               //todo 写入日志 -- 定时发送时间不是有效的时间格式
               return ['status' => -6,'msg' => '定时发送时间不是有效的时间格式'];
           case -7 :
               //todo 写入日志 -- 禁止10小时以内向同一手机号发送相同短信
               return ['status' => -7,'msg' => '禁止10小时以内向同一手机号发送相同短信'];
       }
    }

    function get_oneself($getUrl){
        //$getUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.APPID.'&secret='.SECRET;
        $this_header = array(
            "content-type: application/x-www-form-urlencoded;
charset=GBK"
        );
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);
        curl_setopt($ch, CURLOPT_URL, $getUrl);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($ch, CURL_SSLVERSION_SSL, 2);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $data = curl_exec($ch);
        $response = json_decode($data,true);
        curl_close($ch);
        return $response;
    }

    public function __destruct(){
        unset($this->sms_apim);
        unset($this->sms_crop_id);
        unset($this->sms_pwd);
        unset($this->sms_title);
    }


  

    

   

}